package com.sgb.goods.view.activity.vpm;

import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityVipPriceManageBinding;
import com.sgb.goods.entity.IpPickSelectEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.entity.ProListEntity;
import com.sgb.goods.entity.StorageManEntity;
import com.sgb.goods.entity.VipPriceMangeListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.SmartUtils;
import com.sgb.goods.utils.StringUtils;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.view.adapter.publish.pop.GoodsTypePopWindow;
import com.sgb.goods.view.adapter.publish.pop.StoragePopWindow;
import com.sgb.goods.viewmodel.VipPriceManageVieModel;
import com.sgb.goods.widget.DialogHelper;
import com.sgb.goods.widget.pop.GoodsStatusPopWindow;
import com.swgk.core.dialog.RoundDialog;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 会员价管理
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_VIP_PRICE_MANAGE)
public class VipPriceManageActivity extends BaseActivity<GoodsActivityVipPriceManageBinding, VipPriceManageVieModel>
        implements SmartRefreshLayoutHelper {

    private StoragePopWindow storagePopWindow;
    private GoodsTypePopWindow goodsTypePopWindow;
    private GoodsStatusPopWindow goodsStatusPopWindow;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_vip_price_manage;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(VipPriceManageVieModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        KeyboardUtils.hideSoftInput(mDataBinding.editSearch);
        mViewModel.getData(false);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mViewModel.getData(false);
    }

    @Override
    protected void initListener() {
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            VipPriceMangeListEntity entity = mViewModel.adapter.getData().get(position);
            if (R.id.tv_btn == view.getId() && null != entity) {
                if (1 == entity.status) {
                    DialogHelper.showDialog(VipPriceManageActivity.this, "温馨提示",
                            "结束会员价后商品恢复原价，是否确认结束该商品会员价！", "确定",
                            new DialogHelper.DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    mViewModel.finishItemMemberPrice(entity.id, entity.itemMemberId);
                                }
                            });
                } else {
                    mViewModel.againItemMemberPrice(entity.id, entity.itemMemberId);
                }
            }
        });

        // 跳转选择会员价商品
        mDataBinding.tvAddnewvipprice.setOnClickListener(v -> {
            ARouter.getInstance().build(Constants.ARouter.AR_CHOICE_VIP_PRICE_GOODS).navigation();
        });

        mDataBinding.backView.setOnClickListener(v -> finish());

        // 选择会员价商品状态
        mDataBinding.llChoiceType.setOnClickListener(v -> {
            if(!KeyboardUtils.isSoftInputVisible(VipPriceManageActivity.this)) {
                mViewModel.isOpenGoodsStatus.set(true);
                showGoodsStatusPopWindow();
            } else {
                KeyboardUtils.hideSoftInput(mDataBinding.editSearch);
            }
        });

        // 选择仓储地址列表
        mDataBinding.llChoiceStorage.setOnClickListener(v -> {
                String searchContent = null == storagePopWindow ? "" : StringUtils.convertEmptyForStr(storagePopWindow.contentText);
                mViewModel.getStorageList(searchContent, false);
        });

        // 获取商品分类数据
        mDataBinding.linChoiceClass.setOnClickListener(v -> mViewModel.getGoodsCategoryModel());

        mDataBinding.editView.setOnClickListener(v -> {
            mViewModel.getData(false);
        });

        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getData(true);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                mViewModel.getData(false);
            }
        });

        mViewModel.adapter.setOnItemClickListener((adapter, view, position) -> {
            ARouter.getInstance().build(Constants.ARouter.AR_MEMBER_PRICE_DETAILS)
                    .withLong("itemId", mViewModel.adapter.getItem(position).id)
                    .withLong("itemMemberId", mViewModel.adapter.getItem(position).itemMemberId).navigation();
        });
    }

    @Override
    public void initObserve() {
        mViewModel.listData.observe(this, listData -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, listData);
        });

        mViewModel.storageList.observe(this, storageList -> {
            mViewModel.isOpenStorage.set(true);
            showStoragePopWindow(storageList);
        });

        mViewModel.menuEntityList.observe(this, menuEntityList -> {
            mViewModel.isOpenGoodsType.set(true);
            showGoodsClassPopWindow(menuEntityList);
        });

        mViewModel.dialog_show_text.observe(this, data -> {
            new RoundDialog.Builder(VipPriceManageActivity.this)
                    .title("提示")
                    .content(data)
                    .confirm("知道了").confirmCallback(v -> mViewModel.getData(false)).show();
        });
    }

    /** 选择商品状态下拉弹窗 */
    private void showGoodsStatusPopWindow() {
        if (goodsStatusPopWindow == null) {
            goodsStatusPopWindow = new GoodsStatusPopWindow(this);
            goodsStatusPopWindow.setData(mViewModel.choiceTypeList);
            goodsStatusPopWindow.setOnDismissListener(() -> {
                mViewModel.isOpenGoodsStatus.set(false);
            });
            goodsStatusPopWindow.setOnResultClick((type, key2, key3) -> {
                mDataBinding.tvType.setText(((IpPickSelectEntity) type).name);
                mViewModel.itemMemberStatus.set(((IpPickSelectEntity) type).id);
                mViewModel.getData(false);
            });
        }
        KeyboardUtils.hideSoftInput(mDataBinding.editSearch);
        goodsStatusPopWindow.showAsDropDown(mDataBinding.line, 0, SmartUtils.getHeight0_5DpToPx());
    }

    /**
     * 选择仓储地址
     */
    private void showStoragePopWindow(ProListEntity<List<StorageManEntity>> storageList) {
        if (storagePopWindow == null) {
            storagePopWindow = new StoragePopWindow(this);
            storagePopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            storagePopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            storagePopWindow.inputTextInterface(contentText -> mViewModel.getStorageList(contentText, false));
            storagePopWindow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                    mViewModel.getStorageList(storagePopWindow.contentText.get(), true);
                }

                @Override
                public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                    mViewModel.getStorageList(storagePopWindow.contentText.get(), false);
                }
            });
            storagePopWindow.setOnDismissListener(() -> {
                mViewModel.storage.set(mViewModel.storage.get());
                mViewModel.isOpenStorage.set(false);
            });
            storagePopWindow.setOnResultClick((key1, key2, key3) -> {
                StorageManEntity entity = (StorageManEntity) key1;
                mViewModel.storage.set(entity.storageName);
                mViewModel.storageId.set(entity.no);
                mViewModel.getData(false);
            });
        }
        KeyboardUtils.hideSoftInput(mDataBinding.editSearch);
        storagePopWindow.hasAddStorage.set(false);
        storagePopWindow.setData(storageList.getList(), storageList.isLoadMore);
        storagePopWindow.showAsDropDown(mDataBinding.rlBoot);
    }

    /**
     * 选择商品分类
     */
    public void showGoodsClassPopWindow(List<MenuEntity1<MenuEntity1<MenuEntity1>>> menuEntityList) {
        if (null == goodsTypePopWindow) {
            goodsTypePopWindow = new GoodsTypePopWindow(VipPriceManageActivity.this);
            goodsTypePopWindow.setOnDismissListener(() -> {
                mViewModel.cid3.set(mViewModel.cid3.get());
                mViewModel.isOpenGoodsType.set(false);
            });
            goodsTypePopWindow.setOnResultClick((key1, key2, key3) -> {
                mViewModel.cid1.set(key1 != null ? ((MenuEntity1) key1).id : "");
                mViewModel.cid3.set(key2 != null ? ((MenuEntity1) key3).id : "");
                mViewModel.getData(false);
            });
        }
        KeyboardUtils.hideSoftInput(mDataBinding.editSearch);
        goodsTypePopWindow.setData(menuEntityList);
        goodsTypePopWindow.showAsDropDown(mDataBinding.rlBoot);
    }
}
